// 时间复杂度为O(N^2)先做出来，混个积分
var maxProduct = function (nums) {
  let max = (nums[0] - 1) * (nums[1] - 1)
  for (let i = 0; i < nums.length - 1; i++) {
    for (let j = i + 1; j < nums.length; j++) {
      let temp = (nums[i] - 1) * (nums[j] - 1);
      max = temp > max ? temp : max
    }
  }
  return max;
};

// 所有元素是大于0的，选取两个最大的数即可
var maxProduct1 = function (nums) {
  let firstMax = secondMax = 0;
  if (nums[0] < nums[1]) {
    firstMax = nums[1];
    secondMax = nums[0];
  } else {
    firstMax = nums[0];
    secondMax = nums[1];
  }
  for (let i = 2; i < nums.length; i++) {
    if (nums[i] > firstMax) {
      secondMax = firstMax
      firstMax = nums[i];
    } else if (nums[i] > secondMax) {
      secondMax = nums[i]
    }
  }
  return (firstMax - 1) * (secondMax - 1);
};
// console.log(maxProduct1([3, 4, 5, 2]));

console.log(maxProduct1([3, 4, 5, 2]));
console.log(maxProduct1([1, 5, 4, 5]));